package org.ehe.business.overdue.controller;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.business.overdue.domain.OverdueRecordHistory;
import org.ehe.business.overdue.domain.bo.OverdueRecordBo;
import org.ehe.business.overdue.domain.vo.OverdueRecordVo;
import org.ehe.business.overdue.service.OverdueRecordService;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.idempotent.annotation.RepeatSubmit;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 响应超时预警管理
 * @author :zhangnn
 * @className :OverdueController
 * @description: TODO
 * @date 2025-10-14 16:20:26
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/overdue")
public class OverdueRecordController extends BaseController {
    private final OverdueRecordService recordService;

    /**
     * 分页列表
     */
    //@SaCheckPermission("business:overdue:list")
    @GetMapping("/list")
    public TableDataInfo<OverdueRecordVo> list(OverdueRecordBo bo, PageQuery pageQuery) {
        return recordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出
     */
    //@SaCheckPermission("business:overdue:export")
    @Log(title = "超期记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OverdueRecordBo bo, HttpServletResponse response) {
        List<OverdueRecordVo> list = recordService.queryList(bo);
        ExcelUtil.exportExcel(list, "超期记录", OverdueRecordVo.class, response);
    }

    /**
     * 详情
     */
    //@SaCheckPermission("business:overdue:query")
    @GetMapping("/{id}")
    public R<OverdueRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return R.ok(recordService.queryById(id));
    }

    /**
     * 新增
     */
    //@SaCheckPermission("business:overdue:add")
    @Log(title = "超期记录", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OverdueRecordBo bo) {
        return toAjax(recordService.insertByBo(bo));
    }

    /**
     * 修改
     */
    //@SaCheckPermission("business:overdue:edit")
    @Log(title = "超期记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OverdueRecordBo bo) {
        return toAjax(recordService.updateByBo(bo));
    }

    /**
     * 删除
     */
    //@SaCheckPermission("business:overdue:remove")
    @Log(title = "超期记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(recordService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 切换处理状态
     */
    @Log(title = "超期记录", businessType = BusinessType.UPDATE)
    @PutMapping("/status/{id}")
    public R<Void> changeStatus(@PathVariable Long id,
                                @RequestParam Integer status) {
        return toAjax(recordService.updateHandleStatus(id, status));
    }

    /**
     * 查询提醒历史
     * @param id
     * @return
     */
    @GetMapping("/overdueHistory/{id}")
    public R<List<OverdueRecordHistory>> overdueHistory(@PathVariable Long id) {
        return R.ok("提醒历史获取成功",recordService.queryOverdueHistory(id));
    }
}
